package org.springblade.common.intercepter;

import lombok.AllArgsConstructor;
import org.springblade.modules.app.entity.AppUser;
import org.springblade.modules.app.service.IAppUserService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by admin on 2019/4/8.
 */
@AllArgsConstructor
public class WebInterceptor implements HandlerInterceptor {
	IUserService iUserService;
	IAppUserService appUserService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		AppUser userId = (AppUser) (request.getSession().getAttribute("user"));
		if (userId == null) {
			userId = appUserService.getUserByCookie(request, response);
			if (userId == null) {
				String path = request.getRequestURI();
				request.getSession().setAttribute("path", path);
				response.sendRedirect("/web/login");
				//	request.getRequestDispatcher("/web/login").forward(request, response);
				return false;
			}
			request.getSession().setAttribute("user", userId);
		}
		request.setAttribute("user", userId);
		request.setAttribute("userName", userId.getNickName());

		return true;
	}
}
